package server

import (
	"strconv"
	"time"

	"github.com/tidwall/gjson"
)

// 返回结构
type ResponseData struct {
	Route  interface{} `json:"route"`
	Time   interface{} `json:"time"`
	Status interface{} `json:"status"`
	Data   interface{} `json:"data"`
}

// 正确返回
func ResponseJson(data interface{}) interface{} {

	status := make(map[string]interface{})
	status["code"] = 1
	status["msg"] = "SUCCESS"

	ms := time.Now().UnixNano() / 1e6
	timestamp := strconv.FormatInt(ms, 10)

	resData := make(map[string]interface{})
	resData["route"] = "json"
	resData["time"] = timestamp
	resData["status"] = status
	resData["data"] = data

	resJson := &ResponseData{resData["route"], resData["time"], resData["status"], resData["data"]}

	return resJson
}

// 错误返回
func ResponseErr(code int) interface{} {

	// 文件名
	errCodeFile := "errCode.json"

	// 获取错误信息
	errCodeInfo := GetFile(errCodeFile)

	// 获取错误信息
	strCode := strconv.Itoa(code)
	errInfo := gjson.Get(errCodeInfo, strCode)
	msg := errInfo.String()

	// 返回信息
	status := make(map[string]interface{})
	status["code"] = code
	status["msg"] = msg

	ms := time.Now().UnixNano() / 1e6
	timestamp := strconv.FormatInt(ms, 10)

	resData := make(map[string]interface{})
	resData["route"] = "err"
	resData["time"] = timestamp
	resData["status"] = status
	resData["data"] = ""

	resJson := &ResponseData{resData["route"], resData["time"], resData["status"], resData["data"]}

	return resJson
}
